package com.smartcruise.util.network.callBack;

import com.smartcruise.MyApplication;
import com.smartcruise.R;
import com.smartcruise.base.BaseNormalView;
import com.smartcruise.util.Key;
import com.smartcruise.util.network.customerObserver.NetErrorUtils;
import com.smartcruise.util.network.netUtils.Result;

/**
 * Created by FengChaoQun
 * on 2018/2/5 0005
 * 简单的BaseCallBack子类 完成了错误和异常的处理 使其子类只需要关注与onSuccess这种情况
 */

public abstract class SimpleCallBack implements BaseCallBack {

    private BaseNormalView baseNormalView;

    public SimpleCallBack(BaseNormalView baseNormalView) {
        this.baseNormalView = baseNormalView;
    }

    @Override
    public void onFail(Result result) {
        if (result == null) {
            baseNormalView.showToast(baseNormalView.getActivity().getString(R.string.emptyDataFromNet));
        } else {
            baseNormalView.showToast(result.getMessage());
            //token过期
            if (Key.expiredToken.equals(result.getMessage())) {
                MyApplication.getInstance().showExpiredDialog();
            }
        }
    }

    @Override
    public void onError(Throwable e) {
        e.printStackTrace();
        baseNormalView.showToast(NetErrorUtils.getErrorNotice(e));
    }
}
